<?php
class Order extends Eloquent {
	protected $table = 'order';

	const STATE_INIT = 1;//订单状态：初始状态(待付款)
	const STATE_CANCEL = 2;//订单状态：取消
	const STATE_PAID   = 3;//订单状态：已付款,未完成
	const STATE_DONE   = 4;//订单状态：完成

	const PAY_STATUS_INIT = 0;
	const PAY_STATUS_PAIS = 1;

	const ORDER_TYPE_REWARD  = 'reward';

    use SoftDeletingTrait;
    protected $dates = ['deleted_at'];

	protected $guarded = array('id', 'order_no');
	protected $fillable = [
		'user_id',
		'price',
		'title',
		'phone',
		'qty',
		'workflow_id',
		'state',
		'pay_status',
		'order_type',
		'product_id',
	];

	public function items()
	{
		return $this->hasMany('OrderItem');
	}

	/*public function workflows()
	{
		return $this->hasMany('Workflow');
	}*/
	public function workflow()
	{
		return   Workflow::where('order_id',$this->id)->orderBy('id','desc')->firstOrFail();
	}
	public function user()
	{
		return $this->belongsTo('User');
	}
	public function address()
	{
		return $this->belongsTo('OrderAddress','address_id','id');
	}
	public function workflows()
	{
		return $this->hasMany('Workflow');
	}
	protected static function boot()
	{
		parent::boot();
		static::creating( function($model){
			$model->order_no = $model->generateNo();
		});
	}

	public function generateNo()
	{
		$ua = empty($_SERVER['HTTP_USER_AGENT'])?'':$_SERVER['HTTP_USER_AGENT'];
		$pref = 'P';
		if ( strpos($ua, 'Android')  || strpos($ua,'BlackBerry') || strpos($ua, 'IEMobile')
				|| strpos($ua, 'Opera Mini') || strpos($ua,' UCWEB') || strpos($ua, 'Opera Mobi')
				|| strpos($ua, 'iPhone') || strpos($ua, 'Windows Phone')){
			$pref = 'M';
		}
		$date  = date('YmdHis');
		$user_id = $this->user_id;
		$subfix = ($date/(1+$user_id)) & 7 ;
		return $pref . $date . '-' . $user_id.$subfix . rand(0, 9);
	}

	/*
	 * 个人订单
	 */
	public static function getOrder($orderState = null)
	{

		if($orderState){
			$orders = Order::where("user_id", Auth::user()->id)->where('state',$orderState)->orderBy('created_at', 'DESC')->get();
		}else{

			$orders = Order::where("user_id", Auth::user()->id)->orderBy('created_at', 'DESC')->get();

		}
		$datas = array();

		foreach($orders AS $order)
		{

			$order_id = $order->id;
			$item_count = App::make('OrderItem')->where('order_id', $order_id)->count();
			$first_item = OrderItem::where('order_id', $order_id)->whereHas('product',function($q){
				$q->where('id','>',0);
			})->first();
			$first_product = $first_item->product;
			$datas[$order_id] = array(
						'order_no' => $order->order_no,
						'order_status' =>  $order->workflow()->node->name,
						'order_memo' => $order->memo,
						'total_price' => $order->price,
						'product_id' => $first_product->id,
						'product_img' => $first_product->thumb,
						'product_name' => $first_product->name,
						'product_price' => $first_item->price,
						'product_qty' => $first_item->qty,
						'item_count' => $item_count,
						'created_at' => $order->created_at,
						'updated_at' => $order->updated_at
				);
		}
		return $datas;

	}

	/*
	 * 全部订单
	 */
	public static function allOrder(){

		return self::getOrder(null);
	}

	/*
	 * 代付款订单
	*/
	public static function waitPayOrder(){
		return self::getOrder(self::STATE_INIT);
	}
	/*
	 * 待评价订单
	*/
	public static function waitEvaluationOrder(){
		return self::getOrder(self::STATE_PAID);
	}

	/*
	 * 已完成订单
	*/
	public static function completeOrder(){
		return self::getOrder(self::STATE_DONE);
	}

	/*
	 * 律师订单
	*/
	public static function lawyerOrder($orderState, $user_id=null) {
		if(!$user_id) $user_id = Auth::user()->id;
			$order = Order::leftjoin('order_item', 'order.id', '=', 'order_item.order_id')
			->leftjoin('product', 'order_item.product_id', '=', 'product.id')
			->leftjoin('user', 'user.id', '=', 'order.user_id')
			->select(
					'order_item.qty',
					'order_item.price',
					'product.name',
					'order.order_no',
					'order.memo',
					'order.workflow_id',
					'order.pay_status',
					'order.created_at',
					'order.updated_at',
					'user.realname')
					->where('product.creator_id', '=', $user_id)->where('order.state', '=', $orderState)->get();
			return $order;
	}

}